<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Manage;
use App\Personal\jwt;
use Illuminate\Http\Request;
use function App\Helpers\createToken;
use function App\Helpers\errorMsg;
use function App\Helpers\successMsg;

class LoginController extends Controller
{
    public function doLogin(Request $request)
    {
        $account = $request->account ?? null;
        $password = $request->password ?? null;
        if(empty($account) || empty($password)) return errorMsg(-1,"账号或密码不能为空");
        $manage = Manage::query()->where('nick_name',$account)
            ->orWhere('account',$account)
            ->orWhere('mobile',$account)
            ->first();
        if(($manage['password'] != md5(md5($password))) || empty($manage)) return errorMsg(-1,"账号或密码不正确");
        $token = (new jwt())->aud('manage')->sub($manage->id)->get_token();
        $manage->token = $token;
        $manage->save();
        return successMsg($manage);
    }
    public function checkToken(Request $request)
    {
        /*$jwt = new jwt();
        $res = $jwt->parse_toekn($request);//检测token是否为空
        if(is_array($res)) return $res;
        $res = $jwt->check_sign();//检测token是否正确
        if(is_array($res)) return $res;
        $res = $jwt->check();//检测token是否过期
        if(is_array($res)) return $res;
        if($jwt->body_arr->aud != 'manage'){
            return false;
        }
        return $jwt->body_arr->sub;*/
        return $request->manage_id ?? 0;
    }
}
